package com.open.source.ams.metadata;

import com.open.source.ams.annotation.ExcelEntityProperty;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.NoArgsConstructor;

/**
 * @author ZonLen since on 2022/3/10 下午2:29
 */
@NoArgsConstructor
public class ImportPropertyMetadata {

  @NotBlank(message = "属性名不能为空")
  private String propertyName;

  @NotNull(message = "属性类型不能为空")
  private String propertyType;
  /**
   * 行索引
   */
  private Integer rowIndex;

  @NotNull(message = "解析列索引不能为空")
  private Integer columnIndex;

  private boolean importProperty;
  /**
   * 属性javax注解元数据
   */
  private final List<ValidationPropertyAnnotationMetadata> validationPropertyAnnotationMetadataList = new ArrayList<>();

  public ImportPropertyMetadata(Field propertyField) {
    final ExcelEntityProperty excelEntityProperty = propertyField
        .getAnnotation(ExcelEntityProperty.class);
    importProperty = excelEntityProperty != null;
    if (!importProperty) {
      return;
    }
    this.propertyName = propertyField.getName();
    this.propertyType = propertyField.getType().getName();
    this.columnIndex = excelEntityProperty.index();
    for (Annotation annotation : propertyField.getAnnotations()) {
      final ValidationPropertyAnnotationMetadata validationPropertyAnnotationMetadata = new ValidationPropertyAnnotationMetadata(annotation);
      if (validationPropertyAnnotationMetadata.isJavaxAnnotation()){
        validationPropertyAnnotationMetadataList.add(validationPropertyAnnotationMetadata);
      }
    }
  }

  public String getPropertyName() {
    return propertyName;
  }

  public String getPropertyType() {
    return propertyType;
  }

  public Integer getRowIndex() {
    return rowIndex;
  }

  public Integer getColumnIndex() {
    return columnIndex;
  }

  public List<ValidationPropertyAnnotationMetadata> getValidationPropertyAnnotationMetadataList() {
    return validationPropertyAnnotationMetadataList;
  }

  public String joiningValidateAnnotationType(String joining) {
    return validationPropertyAnnotationMetadataList.stream()
        .map(ValidationPropertyAnnotationMetadata::getValidateAnnotationType).collect(
            Collectors.joining(joining));
  }

  public boolean isImportProperty() {
    return importProperty;
  }
}
